package com.mxr.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.plugins.Page;
import com.mxr.model.Users;
import com.mxr.service.user.UserService;

/**
 * 简易版的Dome
 */
@Controller
@RequestMapping(value = "/dome")
public class DomeController {

	private Logger logger = LoggerFactory.getLogger(DomeController.class);
	
	@Autowired
	private UserService userService;
	
	@GetMapping("/select")
	public String index (Model model) {
		logger.info("查询User列表信息");
		Page<Users> pages = userService.selectUserPage(new Page<Users>(1, 10), null);
		model.addAttribute("pages", pages);
		return "dome";
	}
	
	@GetMapping("/select/{page}")
	public String selectUser (Model model,@PathVariable Integer page) {
		logger.info("查询User列表信息");
		Page<Users> pages = userService.selectUserPage(new Page<Users>(page, 10), null);
		model.addAttribute("pages", pages);
		return "dome";
	}
	
	@GetMapping("/get/{id}")
	public String get (Model model, @PathVariable String id) {
		logger.info("根据Id查询Users用户信息");
		Users user = userService.selectById(id);
		model.addAttribute("user", user);
		return "update";
	}
	
	@PostMapping("/update")
	public String update (Users user) {
		logger.info("修改Users用户信息");
		boolean b = userService.updateById(user);
		
		if (b) {
			return "redirect:select";
		} else {
			return "update";
		}
	}
	
	@GetMapping("/delete/{id}")
	public String delete (@PathVariable String id) {
		logger.info("删除Users用户信息");
		userService.deleteById(id);
		return "redirect:select";
	}
}
